<template>
  <a-spin :spinning="confirmLoading">
    <a-form-model ref="form" :model="model" :rules="validatorRules">
      <a-row>
        <a-col style="margin-bottom: 15px" v-for="(item, index) in promoteRules" :key="index">
          <i18n path="promote-rules" tag="span">
            <a-select
              place="goodsForm"
              v-model="item.goodsForm"
              style="width: 140px; margin: 0 5px"
              :placeholder="$t('请选择')"
              :disabled="item.disable && !disableSubmit"
              @change="(value) => handleGoodsChange(value, index)"
            >
              <a-select-option v-for="good in goodsFormList" :key="good.value">{{
                good.title
              }}</a-select-option>
            </a-select>
            <a-select
              place="goodsId"
              v-model="item.goodsId"
              show-search
              style="width: 360px; margin: 0 5px"
              :placeholder="$t('请下拉或者输入商品名称选择商品')"
              :filter-option="false"
              :not-found-content="null"
              :disabled="(item.disable && !disableSubmit) || (!(item.disable && !disableSubmit) && !item.goodsForm)"
              @search="(val) => handleGoodsSearch(val, item)"
            >
              <a-select-option v-for="good in addGoodsList" :key="good.id" :value="good.id">{{ good.name }}</a-select-option>
            </a-select>
            <a-input
              place="amount"
              v-model="item.amount"
              :disabled="item.disable && !disableSubmit"
              style="width: 70px; margin: 0 5px"
            ></a-input>
          </i18n>
          <a href="javascript:void(0);" style="margin-left: 8px" v-if="item.disable && !disableSubmit"
            ><a-icon type="edit" @click="editPromoteRule(index)"
          /></a>
          <a href="javascript:void(0);" style="margin-left: 8px" v-if="!item.disable && !disableSubmit"
            ><a-icon type="check" @click="savePromoteRule(item, index)"
          /></a>
          <a href="javascript:void(0);" style="margin-left: 8px" v-if="!disableSubmit"
            ><a-icon type="close" @click="deletePromoteRule(item, index)"
          /></a>
          <a href="javascript:void(0);" style="margin-left: 8px" v-if="index == promoteRules.length - 1 && !disableSubmit"
            ><a-icon type="plus" @click="addPromoteRule"
          /></a>
        </a-col>
      </a-row>
    </a-form-model>
  </a-spin>
</template>

<script>
import { isEmpty1 } from '@/api/comm'
import { httpAction, getAction, deleteAction } from '@/api/manage'
import { initDictOptions } from '@/components/dict/JDictSelectUtil'

export default {
  name: 'RulePromoteForm',
  components: {
  },
  data () {
    return {
      model:{},
      disableSubmit: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
      },
      url: {
        add: "/member/bisMemberRuleGoods/add",
        edit: "/member/bisMemberRuleGoods/edit",
        delete: "/member/bisMemberRuleGoods/delete",
        queryById: "/member/bisMemberRuleGoods/queryById"
      },
      promoteRules: [],
      goodsFormList: [],
      addGoodsList: []
    }
  },
  created () {
     //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    edit(record) {
      this.visible = true
      initDictOptions('bis_goods_infor.form').then((res) => {
        if (res.success) {
          this.goodsFormList = res.result.filter(item=>{
            return item.value != '5' && item.value != '7' && item.value != '8' && item.value != '10' && item.value != '11' 
            && item.value != '12' && item.value != '13' && item.value != '16' && item.value != '17'
          })
        }
      })
      this.addGoodsList = []
      this.model = Object.assign({}, record)
      this.getPromoteRule()
    },
    getPromoteRule() {
      let that = this
      getAction('/member/bisMemberRuleGoods/list', { memberRuleId: that.model.id, pageSize: 1000 }).then((res) => {
        if (res.success) {
          let records = res.result.records
          records.forEach((rd) => {
            rd.disable = true
            let dtl = {
              id: rd.goodsId,
              form: rd.goodsForm,
              name: rd.goodsId_dictText
            }
            that.addGoodsList.push(dtl)
          })
          that.promoteRules = records
        } else {
          that.$message.error(res.message)
        }
        if (!that.disableSubmit) {
          that.promoteRules.push({
            memberRuleId: that.model.id,
            disable: false,
            goodsForm: undefined,
            amount: 1,
            goodsId: undefined,
          })
        }
      })
    },
    addPromoteRule() {
      this.promoteRules.push({
        memberRuleId: this.model.id,
        goodsForm: undefined,
        amount: 1,
        goodsId: undefined,
        disable: false,
      })
    },
    deletePromoteRule(item, i) {
      let that = this
      if (item.id) {
        deleteAction(that.url.delete, { id: item.id }).then((res) => {
          if (res.success) {
            that.$message.success(res.message)
            that.getPromoteRule()
          } else {
            that.$message.warning(res.message)
          }
        })
      } else if (that.promoteRules.length - 1 > 0) {
        that.promoteRules.splice(i, 1)
      }
    },
    editPromoteRule(i) {
      this.$set(this.promoteRules[i], 'disable', false)
    },
    savePromoteRule(item, i) {
      let that = this
      if (isEmpty1(item.goodsForm)) {
        that.$message.warn(that.$t('请选择商品形态'))
      } else if (isEmpty1(item.goodsId)) {
        that.$message.warn(that.$t('请选择商品'))
      } else if (item.amount === '0' || item.amount === undefined || item.amount === '') {
        that.$message.warn(that.$t('请输入数量'))
      } else if (isNaN(item.amount)) {
        that.$message.warn(that.$t('数量必须是数值'))
      } else {
        that.$set(that.promoteRules[i], 'disable', true)
        let httpurl = ''
        let method = ''
        if (!item.id) {
          httpurl += that.url.add
          method = 'post'
        } else {
          httpurl += that.url.edit
          method = 'put'
        }
        let query = Object.assign({}, item)
        httpAction(httpurl, query, method).then((res) => {
          if (res.success) {
            that.$set(item, 'disabled', true)
            that.getPromoteRule()
            that.$message.success(res.message)
          } else {
            that.$message.warning(res.message)
          }
        })
      }
    },
    handleGoodsChange(event) {
      this.getGoodsListTwo(event)
    },
    handleGoodsSearch(value, record) {
      this.getGoodsListTwo(record.goodsForm, value)
    },
    getGoodsListTwo(form, value) {
      let querydata = {}
      querydata.pageIndex = 1
      querydata.pageSize = 10
      querydata.form = form
      querydata.goodsInfo = value
      getAction('/goods/bisGoodsInfor/list', querydata).then((res) => {
        if (res.success) {
          this.addGoodsList = res.result.records
        }
      })
    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
.cus-form {
  margin-top: 10px;
  margin-left: 40px;
}
</style>